<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="淘汰编号" prop="eliminateCode">
        <el-input
          v-model="queryParams.eliminateCode"
          placeholder="请输入淘汰编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="person">
        <el-input
          v-model="queryParams.person"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="淘汰日期" prop="eliminateDate">
        <el-date-picker clearable
                        v-model="queryParams.eliminateDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择淘汰日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:vendorEliminate:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['md:vendorEliminate:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['md:vendorEliminate:remove']"
              >删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['md:vendorEliminate:export']"
              >导出
              </el-button>
            </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--    <el-table v-loading="loading" :data="eliminateList" @selection-change="handleSelectionChange">-->
    <el-table v-loading="loading" :data="eliminateList">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="淘汰单编号" align="center" prop="eliminateCode"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="vendorName"/>
      <el-table-column label="淘汰原因" align="center" prop="eliminateReason"/>
      <el-table-column label="淘汰日期" align="center" prop="eliminateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eliminateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否加入黑名单" align="center" prop="isBlacklist">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isBlacklist"/>
      </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="person"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['md:vendorEliminate:query']"
            >查看
            </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:vendorEliminate:edit']"
            v-show="scope.row.status==='PREPARE' || scope.row.status==='DISALLOW'"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:vendorEliminate:remove']"
            v-show="scope.row.status==='PREPARE' || scope.row.status==='DISALLOW'"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商淘汰对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="淘汰单编号" prop="eliminateCode">
              <el-input v-model="form.eliminateCode" :readonly="optType!='add'"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="vendorName">
              <el-input v-model="form.vendorName" readonly="readonly" placeholder="请选择供应商">
                <el-button slot="append" @click="handleSelectVendor" icon="el-icon-search"></el-button>
              </el-input>
              <VendorSelect ref="vendorSelect" @onSelected="onVendorSelected"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商地址" prop="vendorAddress">
              <el-input v-model="form.vendorAddress" placeholder="请输入供应商地址" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="vendorContact">
              <el-input v-model="form.vendorContact" placeholder="请输入供应商联系人" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="vendorContactTel" >
              <el-input v-model="form.vendorContactTel" placeholder="请输入供应商联系人电话" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作人" prop="person">
              <el-input v-model="form.person" placeholder="请选择操作人" readonly>
                <el-button slot="append" @click="handleSelectUser" icon="el-icon-search"></el-button>
              </el-input>
              <UserSelect ref="userSelect" @onSelected="onUserSelected"></UserSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="淘汰日期" prop="eliminateDate">
              <el-date-picker clearable
                              v-model="form.eliminateDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择淘汰日期"
                              style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="淘汰原因" prop="eliminateReason">
              <el-input v-model="form.eliminateReason" placeholder="请输入淘汰原因"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否加入黑名单">
              <el-radio-group v-model="form.isBlacklist">
                <el-radio label="N">否</el-radio>
                <el-radio label="Y">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="附件">
              <FileUpload
                :limit="1"
                :fileSize="100"
                :fileType="fileType"
                :value="fileList"
                @onUploaded="handleUploaded"
                @onRemoved="handleRemoved">
              </FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button type="primary" @click="confirmForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--    查看供应商淘汰对话框-->
    <el-dialog title="查看供应商淘汰" :visible.sync="openView" width="80%" append-to-body>
      <el-form ref="viewForm" :model="form"  label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="淘汰单编号" prop="eliminateCode">
              <el-input v-model="form.eliminateCode" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="vendorName">
              <el-input v-model="form.vendorName" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商地址" prop="vendorAddress">
              <el-input v-model="form.vendorAddress" placeholder="请输入供应商地址" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="vendorContact">
              <el-input v-model="form.vendorContact" placeholder="请输入供应商联系人" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="vendorContactTel" >
              <el-input v-model="form.vendorContactTel" placeholder="请输入供应商联系人电话" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作人" prop="person">
              <el-input v-model="form.person"  readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="淘汰日期" prop="eliminateDate">
              <el-date-picker clearable
                              v-model="form.eliminateDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择淘汰日期"
                              style="width: 100%"
                              readonly>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="淘汰原因" prop="eliminateReason">
              <el-input v-model="form.eliminateReason" placeholder="请输入淘汰原因" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否加入黑名单">
              <el-radio-group v-model="form.isBlacklist" disabled>
                <el-radio label="N">否</el-radio>
                <el-radio label="Y">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="附件">
              <FileUpload
                :limit="1"
                :fileSize="100"
                :fileType="fileType"
                :value="fileList"
                :optType="'view'"
                @onUploaded="handleUploaded"
                @onRemoved="handleRemoved">
              </FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listEliminate, getEliminate, delEliminate, addEliminate, updateEliminate} from "@/api/mes/md/vendorEliminate";
import VendorSelect from "@/components/vendorSelect/single.vue";
import UserSelect from "@/components/userSelect/single.vue";
import {getUserProfile} from "@/api/system/user";
import {genCode} from "@/api/system/autocode/rule"
import view from "@/views/mes/md/drawing/view.vue";

export default {
  name: "Eliminate",
  computed: {
    view() {
      return view
    }
  },
  components: {UserSelect, VendorSelect},
  dicts: ['mes_order_status','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商淘汰表格数据
      eliminateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      optType:undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eliminateCode: null,
        vendorName: null,
        person: null,
        eliminateDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eliminateCode: [
          {required: true, message: "淘汰编号不能为空", trigger: "blur"}
        ],
        vendorName: [
          {required: true, message: "供应商名称不能为空", trigger: "blur"}
        ],
        person: [
          {required: true, message: "操作人姓名不能为空", trigger: "blur"}
        ],
        eliminateDate: [
          {required: true, message: "淘汰日期不能为空", trigger: "blur"}
        ],
        eliminateReason: [
          {required: true, message: "淘汰原因不能为空", trigger: "blur"}
        ],
      },
      // 上传文件类型
      fileType: ["pdf", "jpg", "png", "jpeg", "rar", "zip"],
      // 上传文件列表
      fileList: [],
      user: {},
    };
  },
  created() {
    this.getList();
    this.getUser();
  },
  methods: {
    /** 查询供应商淘汰列表 */
    getList() {
      this.loading = true;
      listEliminate(this.queryParams).then(response => {
        this.eliminateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        eliminateCode: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        vendorAddress: null,
        vendorContact: null,
        vendorContactTel: null,
        personId: null,
        person: null,
        eliminateDate: new Date().toISOString().split('T')[0],
        eliminateReason: null,
        isBlacklist: "N",
        fileUrl: null,
        status: 'PREPARE'
      };
      this.fileList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    /*handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },*/
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.person = this.user.nickName;
      this.form.personId = this.user.userId;
      this.handleAutoGenChange();
      this.open = true;
      this.optType='add';
      this.title = "添加供应商淘汰";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEliminate(id).then(response => {
        this.form = response.data;
        response.data.fileUrl == null ? this.fileList = [] : this.fileList.push(response.data.fileUrl);
        this.open = true;
        this.optType='update';
        this.title = "修改供应商淘汰";
      });
    },
    /** 修改按钮操作 */
    handleView(row) {
      this.fileList=[];
      this.resetForm("viewForm");
      const id = row.id || this.ids
      getEliminate(id).then(response => {
        this.form = response.data;
        response.data.fileUrl == null ? this.fileList = [] : this.fileList.push(response.data.fileUrl);
        this.openView = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEliminate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEliminate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //提交按钮回调
    confirmForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.status='CONFIRMED'
            updateEliminate(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商淘汰编号为"' + row.eliminateCode + '"的数据项？').then(function () {
        return delEliminate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //供应商选择
    handleSelectVendor() {
      this.$refs.vendorSelect.showFlag = true;
    },
    //供应商选择弹出框
    onVendorSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.vendorId = obj.vendorId;
        this.form.vendorCode = obj.vendorCode;
        this.form.vendorName = obj.vendorName;
        this.form.vendorAddress = obj.address;
        if (obj.contact1 != null && obj.contact1Tel != null) {
          this.form.vendorContact = obj.contact1;
          this.form.vendorContactTel = obj.contact1Tel;
        } else {
          this.form.vendorContact = obj.contact2;
          this.form.vendorContactTel = obj.contact2Tel;
        }
      }
    },
    handleSelectUser() {
      this.$refs.userSelect.showFlag = true;
    },
    // 用户选择弹出框
    onUserSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.person = obj.nickName;
        this.form.personId = obj.userId;
      }
    },
    // 获取登录用户
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    handleUploaded(data) {
      this.form.fileUrl = data;
    },
    handleRemoved(data) {
      this.form.fileUrl = data;
    },
    //自动生成编码
    handleAutoGenChange() {
      genCode('VENDOR_ELIMINATE_CODE').then(response => {
        this.form.eliminateCode = response;
      });
    },
    /** 导出按钮操作 */
    /*handleExport() {
      this.download('md/eliminate/export', {
        ...this.queryParams
      }, `eliminate_${new Date().getTime()}.xlsx`)
    }*/
  }
};
</script>
